package com.diy.sigmund.diyuser.test.controller;

import com.diy.sigmund.diycommon.entity.ResultMsg;
import com.diy.sigmund.diycommon.entity.po.user.UserAuth;
import com.diy.sigmund.diycommon.spring.anno.IgnoreTokenVerification;
import com.diy.sigmund.diyuser.entity.dto.UserAuthDTO;
import com.diy.sigmund.diyuser.mapper.UserAuthMapper;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ylm-sigmund
 * @since 2021/8/17 12:04
 */
@RestController
@RequestMapping("/user/mybatisTest")
public class MybatisTestController {

    @Autowired
    private UserAuthMapper userAuthMapper;

    /**
     * <br>### 结论
     * <br>- 其实不管你是查单行记录还是多行记录，对于Mybatis来说都会放到DefaultResultHandler中去，
     * <br>- 而DefaultResultHandler又是用List存储结果，所以不管是集合类型还是普通对象，Mybatis都会先初始化一个List存储结果:
     * <br>- `new DefaultResultHandler(objectFactory)`
     * <br>- 返回值为普通对象(Map也是普通对象)且查为空，selectOne会判断然后直接返回NULL值
     * <br>- 返回值为集合对象且查为空，selectList会把这个存储结果的List对象直接返回，此时这个List就是个空集合
     * <br>
     * <br>
     * <br>### 返回结果为单行数据
     * <br>```tex
     * <br>顺序从上到下:
     * <br>
     * <br>在第一次调用的时候会获得方法的映射属性,再根据返回值走具体策略,具体代码如下
     * <br>    org.apache.ibatis.binding.MapperMethod.MethodSignature#MethodSignature
     * <br>    org.apache.ibatis.binding.MapperMethod#execute
     * <br>查询单个Object
     * <br>    org.mybatis.spring.SqlSessionTemplate#selectOne(java.lang.String, java.lang.Object)
     * <br>org.apache.ibatis.session.defaults.DefaultSqlSession#selectOne(java.lang.String, java.lang.Object)
     * <br>org.apache.ibatis.executor.BaseExecutor#query(org.apache.ibatis.mapping.MappedStatement, java.lang.Object, org.apache.ibatis.session.RowBounds, org.apache.ibatis.session.ResultHandler, org.apache.ibatis.cache.CacheKey, org.apache.ibatis.mapping.BoundSql)
     * <br>org.apache.ibatis.executor.statement.SimpleStatementHandler#query
     * <br>重要:
     * <br>    org.apache.ibatis.executor.resultset.DefaultResultSetHandler#handleResultSets
     * <br>    org.apache.ibatis.executor.resultset.DefaultResultSetHandler#handleResultSet
     * <br>    multipleResults.add(defaultResultHandler.getResultList());
     * <br>org.apache.ibatis.executor.result.DefaultResultHandler#getResultList
     * <br>
     * <br>```
     */
    @IgnoreTokenVerification
    @PostMapping("/selectOne")
    public ResultMsg selectOne() {
        UserAuthDTO userAuthDTO = new UserAuthDTO();
        userAuthDTO.setIdentifier("18727613660");
        userAuthDTO.setCredential("12345622");
        UserAuth login = userAuthMapper.getLogin(userAuthDTO);
        return ResultMsg.success(login);
    }

    @IgnoreTokenVerification
    @PostMapping("/selectList")
    public ResultMsg selectList() {
        String userId = "1428654781064413185";
        // String userId = "xx";
        List<UserAuth> login = userAuthMapper.listByUserId(userId);
        return ResultMsg.success(login);
    }

    @IgnoreTokenVerification
    @PostMapping("/selectMap")
    public ResultMsg selectMap() {
        String userId = "";
        Map<String, String> login = userAuthMapper.testSelectMap(userId);
        return ResultMsg.success(login);
    }
}
